package com.kotiln.lgc.lib_voice.manager

import android.content.Context
import android.util.Log
import com.baidu.speech.EventListener
import com.baidu.speech.asr.SpeechConstant
import com.kotiln.lgc.lib_voice.asr.VoiceAsr
import com.kotiln.lgc.lib_voice.impl.OnAsrResultListener
import com.kotiln.lgc.lib_voice.tts.VoiceTTS
import com.kotiln.lgc.lib_voice.weakup.VoiceWakeUp
import org.json.JSONObject

/**
 *@fileName VoiceManager
 *@date2021/1/19  13:54
 *@author VoiceManager$
 *@describe TODO
 *@desc 语音管理类 面向对象
 */
object VoiceManager : EventListener {

    private var TAG: String? = VoiceManager::class.java.simpleName

    //语音key
    const val VOICE_APP_ID = "23545218"
    const val VOICE_APP_KEY = "Ccoj1N2HI31yMNDz580TXVho"
    const val VOICE_APP_SECRET = "v2q7Y8IoujLQN7hMP63tUfFM9RV83a7q"

    //接口
    private lateinit var mOnAsrResultListener: OnAsrResultListener

    fun initManager(mContext: Context,mOnAsrResultListener: OnAsrResultListener) {
        this.mOnAsrResultListener=mOnAsrResultListener

        VoiceTTS.initTTS(mContext)
        VoiceWakeUp.initWakeUp(mContext, this)
        VoiceAsr.initAsr(mContext, this)
    }

    //----------------TTS START-------------
    //播放
    fun ttsStart(text: String) {
        VoiceTTS.start(text)
    }

    //播放并且监听结果
    fun ttsStart(text: String, mOnTTSResultListener: VoiceTTS.OnTTSResultListener?) {
        VoiceTTS.start(text, mOnTTSResultListener)
    }

    //暂停
    fun ttsPause() {
        VoiceTTS.pause()
    }

    //继续播放
    fun ttsResume() {
        VoiceTTS.resume()
    }

    //停止播放
    fun ttsStop() {
        VoiceTTS.stop()
    }

    //释放资源
    fun ttsRelease() {
        VoiceTTS.release()
    }


    //设置发音人
    fun setPeople(people: String) {
        VoiceTTS.setPeople(people)
    }

    //设置语速
    fun setVoiceSpeed(speed: String) {
        VoiceTTS.setVoiceSpeed(speed)
    }

    //设置音量
    fun setVoiceVolume(volume: String) {
        VoiceTTS.setVoiceVolume(volume)
    }

    //----------------TTS END-------------


    //----------------WAKEUP START-------------

    //启动唤醒
    fun startWeakup() {
        Log.i(TAG, "启动唤醒")
        VoiceWakeUp.startWeakUp()
    }

    //停止唤醒
    fun stopWeakup() {
        Log.i(TAG, "停止唤醒")
        VoiceWakeUp.stopWeakUp()
    }

    //----------------WAKEUP END-------------


    //----------------Asr START-------------
    //开始识别
    fun startAsr() {
        VoiceAsr.startAsr()
    }

    //停止识别
    fun stopAsr() {
        VoiceAsr.stopAsr()
    }

    //取消识别
    fun cancelAsr() {
        VoiceAsr.cancelAsr()
    }

    //销毁识别
    fun releaseAsr() {
        VoiceAsr.releaseAsr(this)
    }

    //----------------Asr END-------------

    override fun onEvent(name: String?, params: String?, byte: ByteArray?, offset: Int, length: Int) {

//        Log.d(TAG, String.format("event: name=%s, params=%s", name, params))

        //语音前置状态
        when (name) {
            SpeechConstant.CALLBACK_EVENT_WAKEUP_READY -> {
                //Log.i(TAG, "唤醒准备就绪")
                mOnAsrResultListener.wakeUpReady()
            }
            SpeechConstant.CALLBACK_EVENT_ASR_BEGIN -> {
                //Log.i(TAG, "开始说话")
                mOnAsrResultListener.asrStartSpeak()
            }
            SpeechConstant.CALLBACK_EVENT_ASR_END -> {
                //Log.i(TAG, "结束说话")
                mOnAsrResultListener.asrStopSpeak()
            }
        }
        //去除脏数据
        if (params == null) {
            return
        }
        val allJson=JSONObject(params)
        when (name) {
            SpeechConstant.CALLBACK_EVENT_WAKEUP_SUCCESS -> {
//                ttsStart("我在")
                mOnAsrResultListener.wakeUpSuccess(allJson)

            }
            SpeechConstant.CALLBACK_EVENT_WAKEUP_ERROR->{
                //Log.i(TAG,"唤醒失败")
                mOnAsrResultListener.voiceError("唤醒失败")
            }
            //SpeechConstant.CALLBACK_EVENT_ASR_READY -> Log.i(TAG,"ASR准备就绪")
            SpeechConstant.CALLBACK_EVENT_ASR_FINISH->{
                Log.i(TAG,"ASR识别结束${params}")
                mOnAsrResultListener.asrResult(allJson)
            }

            SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL ->{

                mOnAsrResultListener.updateUserText(allJson.optString("best_result"))

                byte?.let {
                    val nlu=JSONObject(String(byte,offset,length))
//                    mOnAsrResultListener.asrResult(allJson,nlu)
                    mOnAsrResultListener.nluResult(nlu)
                    //Log.i(TAG,"ASR识别结果:${nlu}")















                }
            }
        }
    }


}